package cn.orange.ch10_dynamicprogramming;

/**
 * LC647.回文子串
 */
public class LC647 {
    public int countSubstrings(String s) {
        int ans = 0;
        int n = s.length();
        //dp[i][j]:s中[i,j]为回文串时,dp[i][j]的值为true
        boolean[][] dp = new boolean[n][n];
        for (int i = n - 1; i >= 0; i--) {
            for (int j = i; j < n; j++) {
                if (s.charAt(i) == s.charAt(j)) {
                    if (j - i <= 1) {
                        dp[i][j] = true;
                        ans++;
                    } else {
                        if (dp[i + 1][j - 1]) {
                            dp[i][j] = true;
                            ans++;
                        }
                    }
                }
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        LC647 alg = new LC647();
        System.out.println(alg.countSubstrings("abc"));
        System.out.println(alg.countSubstrings("aaa"));
    }
}
